package cc.mrbird.febs.buss.util;

import ch.qos.logback.core.net.server.Client;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;

public class ConfigResourceUtil {
    public static String getConfigByName(String name){
        Properties prop = new Properties();
        try{
            String path=FileUtils.getJarPath();
            InputStream is;
            //判断jar根目录下是否存在web_config.properties 如果存在 读取根目录下配置文件 如果不存在 读取resource目录下文件
            if(FileUtils.fileExists(path+"\\web_config.properties")){
                is = new BufferedInputStream(new FileInputStream(path+"\\web_config.properties"));
            }else{
                is=Client.class.getClassLoader().getResourceAsStream("web_config.properties");
            }
            InputStreamReader in = new InputStreamReader(is, "GBK");

            prop.load(in);     ///加载属性列表
            Iterator<String> it=prop.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key=it.next();
                //System.out.println(key+":"+prop.getProperty(key));
            }
            in.close();
            ///保存属性到b.properties文件
            if(prop.getProperty(name)==null||prop.getProperty(name).trim().length()==0){
                return "";
            }
            return new String( prop.getProperty(name).getBytes("utf-8") , "utf-8");
        }
        catch(Exception e){
            System.out.println(e);
        }
        return null;
    }
}
